﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MMDemo
{
    public class BinTree
    {
        private BinTreeNode root;

        /// <summary>
        /// 根节点(属性)
        /// </summary>
        public BinTreeNode Root
        {
            get { return root; }
            set { root = value; }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public BinTree()
        {
            root = null;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="data"></param>
        public BinTree(string data)
        {
            BinTreeNode p = new BinTreeNode(data);
            root = p;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="data"></param>
        /// <param name="ln"></param>
        /// <param name="rn"></param>
        public BinTree(string data, BinTreeNode ln, BinTreeNode rn)
        {
            BinTreeNode p = new BinTreeNode(data, ln, rn);
            root = p;
        }

        /// <summary>
        /// 判断树是否为空
        /// </summary>
        /// <returns></returns>
        public bool IsEmpty()
        {
            if (root == null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 获取节点p的左子节点
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public BinTreeNode GetlChild(BinTreeNode p)
        {
            return p.lChild;
        }

        /// <summary>
        /// 获取节点p的右子节点
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public BinTreeNode GetrChild(BinTreeNode p)
        {
            return p.rChild;
        }

        /// <summary>
        /// 节点p下插入左子节点data
        /// </summary>
        /// <param name="data"></param>
        /// <param name="p"></param>
        public void InsertL(string data, BinTreeNode p)
        {
            BinTreeNode tmp = new BinTreeNode(data);
            tmp.lChild = p.lChild;
            p.lChild = tmp;
        }

        /// <summary>
        /// 节点p下插入右子节点data
        /// </summary>
        /// <param name="data"></param>
        /// <param name="p"></param>
        public void InsertR(string data, BinTreeNode p)
        {
            BinTreeNode tmp = new BinTreeNode(data);
            tmp.rChild = p.rChild;
            p.rChild = tmp;
        }

        /// <summary>
        /// 删除节点p的左子节点
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public BinTreeNode DeleteL(BinTreeNode p)
        {
            if ((p == null) || (p.lChild == null))
            {
                return null;
            }
            BinTreeNode tmp = p.lChild;
            p.lChild = null;
            return tmp;
        }

        /// <summary>
        /// 删除节点p的右子节点
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public BinTreeNode DeleteR(BinTreeNode p)
        {
            if ((p == null) || (p.rChild == null))
            {
                return null;
            }
            BinTreeNode tmp = p.rChild;
            p.rChild = null;

            return tmp;
        }

        /// <summary>
        /// 判断节点p是否为叶节点
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public bool IsLeaf(BinTreeNode p)
        {
            if ((p != null) && (p.lChild == null) && (p.rChild == null))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    }
}
